import {observable,action,toJS} from "mobx";
import * as Apis from "../../apis/docReportApi/docReportApi";
import { SearchAdvanced,RouteLayout } from 'weaver-mobile-page';


class DocReportStore{

  @observable flowCountData = [];
  @observable flowEchart1 = {};
  @observable flowEchart2 = {};
  @observable search = new SearchAdvanced.Store({ route: '/docReport',id:"workflowSearch" });
  @observable docCountSearch = new SearchAdvanced.Store({ route: '/docReport' ,id:"docSearch"});
  @observable detailLayout = new RouteLayout.Store({ route: '/docReport',id:"datail"});
  @observable moreLayout = new RouteLayout.Store({ route: '/docReport'});
  @observable tabKey="workflowCount";
  @observable docEchart1={};
  @observable docEchart2={};
  @observable docEchart3={};
  @observable workflowFormData = {
    dateType: "3",
    orgtype: "2",
    officaltype: "",
    orgid:""
  }
  @observable detailData={}
  @observable showLinkEchart = false;//环节统计是否显示
  @observable docCountData={
    dateType: "3",
    orgtype: "2",
    echartKey:"",
    orgid:""
  };
  @observable loading = false;
  @observable hasRight = true;

  
  @action.bound 
  changeTab(tab){
    this.tabKey = tab.key;
    if(tab.key == "workflowCount"){
      this.getOdocReportCountData();
      this.getOdocReportEchartData();
    }else{
      this.getOdocDocReportEchartData()
      this.getDocCount();
    }    
  }

  @action.bound 
  getCondition(){
    this.search.getData({
      url: '/api/odoc/odocReport/getOdocReportSearchConditionMobile',
      method: 'POST',
    });
  }

  @action.bound
  changeWorkflowStatus(status){
    this.workflowStatus = status;
  }

  //获取流程总数
  @action.bound
  getOdocReportCountData(){
    this.loading = true;
    const params={
      ...this.workflowFormData,
    }
    Apis.getOdocReportCountData(params).then(data => {
      this.hasRight = data.hasRight == false ? false : true;
      this.loading = false;
      if(data.countData){
        this.flowCountData = data.countData;
      }
    })
  }

  //获取流程统计echart图表数据
  @action.bound 
  getOdocReportEchartData(){
    this.loading = true;
    const params={
      ...this.workflowFormData,
    }
    Apis.getOdocReportEchartData(params).then(data=>{
      this.loading = false;
      if(data.title){
        this.getWorkflowSeries(data)
        this.flowEchart1 = data;
        this.flowEchart2 = this.getDocTypeSeries(data.echart2,data.echart2title,"linkCount","x");
      }
    })
  } 

  //获取公文统计echart图表数据
  @action.bound 
  getOdocDocReportEchartData(){
    this.loading = true;
    const params={
      ...this.docCountData,
    }
    Apis.getOdocDocReportEchartData(params).then(data=>{
      this.loading = false;
    if(data.echart1title){
      this.docEchart1 = this.getDocTypeSeries(data.echart1,data.echart1title,"echart1");
      this.docEchart2 = this.getDocTypeSeries(data.echart2,data.echart2title,"echart2");
      this.docEchart3 = this.getDocMonthCount(data.echart3,"echart3");
    }
    })
  } 

  //处理公文流程echarts数据
  getWorkflowSeries(data){
    let series = [];//series
    let endReqData = [];//series归档的data
    let flowReqData = [];//series流转中的data
    let key1 = [];
    let key2 = []; 
    data.flowReqData.map((item,index)=>{
      flowReqData[index] = Number.parseInt(item.value);
      key1[index] = item.key;
    })
    data.endReqData.map((item,index)=>{
      endReqData[index] = Number.parseInt(item.value);
      key2[index] = item.key;
    })
    data.legend.forEach((element,index)=> {
      if(data.legend[index] == "流转中"){
        const serie = {
          type: 'bar',
          name: data.legend[index],
          data: flowReqData,
          stack: '1',//stack相同 堆叠
          status:2,
          dataKey:key1,
          barCategoryGap:'80%',
          itemStyle:{
            color:"#F67C4C"
          },
          id:"flowing"
        }
        series.push(serie)
      }
      if(data.legend[index] == "归档"){
        const serie = {
          type: 'bar',
          name: data.legend[index],
          data: endReqData,
          stack: '1',
          status : 1 ,
          dataKey:key2,
          barCategoryGap:'60%',
          itemStyle:{
            color:"#D65DF4"
          },
          id:"end"
        }
        series.push(serie)
      }
    });
    data.series=series;
  }

  //处理公文种类、主题分类echarts数据
  getDocTypeSeries(data,title,id,axis="y"){
    let valueArray = [];
    let titleArray = [];
    let dataKey = [];
    let echartDatas = {};

    data.map((item)=>{
      valueArray.push(item.title);
      titleArray.push(Number.parseInt(item.value));
      dataKey.push(item.key)
    })
    echartDatas.seriesData = titleArray;
    if(axis == "y"){
      echartDatas.yAxisData = valueArray;
    }else if(axis == "x"){
      echartDatas.xAxisData = valueArray;
    }
    
    echartDatas.title = title;
    echartDatas.id = id;
    echartDatas.dataKey = dataKey;
    return echartDatas;
  }

  //处理月度公文echarts数据
  getDocMonthCount(data,id){
    let seriesData = [];
    let echartDatas = {};
    let dataKey = [];
    data.datas.map(item=>{
      seriesData.push(Number.parseInt(item.value))
      dataKey.push(item.key)
    })
    echartDatas.title = data.title;
    echartDatas.seriesData = seriesData;
    echartDatas.xAxisData = data.xAxisData;
    echartDatas.id = id;
    echartDatas.dataKey = dataKey;
    return echartDatas;
  }


  @action.bound 
  setFormData(params){
    this.workflowFormData = params;
  }
  @action.bound 
  setDetailData(status,dataKey){
    this.detailData = {...this.workflowFormData,status,echartKey:dataKey};
  }

  @action.bound 
  setDocCountData(dataKey){
    this.docCountData = {...this.docCountData,echartKey:dataKey};
  }
  
  @action.bound 
  submit(){
    this.getFormData(toJS(this.search.form.formParams),this.workflowFormData);
    this.getOdocReportCountData(); 
    this.getOdocReportEchartData();
  }

  @action.bound 
  docCountSubmit(){
    this.getFormData(toJS(this.docCountSearch.form.formParams),this.docCountData);
    this.getOdocDocReportEchartData();
  }
  
  @action.bound 
  setOrgid(orgid){
    this.workflowFormData["orgid"] = orgid;
    this.detailData["orgid"] = orgid;
    this.docCountData["orgid"] = orgid;
  }

  @action.bound 
  getFormData(data,formData){
    for(let key in data){
      if(typeof data[key] == "string"){
        formData[key] = data[key];
      }else if(typeof data[key] == "object"){
        if(data[key].length){
          let ids =[];
          for(let i in toJS(data[key])){
            ids.push(data[key][i].id);
          }
          formData[key] = ids.join(",");
        }else{
          this.getFormData(data[key],formData)
        }
      }
    }
  }

  @action.bound
  getDocCount(){
    this.docCountSearch.getData({
      url:"/api/odoc/odocDocReport/getOdocDocReportSearchConditionMobile",
      method:"POST"
    })
  }

  @action.bound
  getUserId(){
    Apis.getUserId().then(datas=>{
      if(datas.api_status){
        this.setOrgid(datas.userDepartmentid);
        this.getOdocReportCountData(); 
        this.getOdocReportEchartData();
        // this.getOdocDocReportEchartData();
      }
    })
  }

  @action.bound 
  setTabKey(tabKey){
    this.tabKey = tabKey;
  }

}

export default DocReportStore;
